/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/**
 * 顺丰城市账号发单
 */

import React, { useRef } from 'react';
import { Button, Switch, message } from 'antd';
import { PageContainer } from '@ant-design/pro-layout';
import DragTable from '@/components/DragTable';
import ProTable from '@ant-design/pro-table';
import { accountSftcList, setSftcCreateShop } from '@/services/jdk/issuance';
import { issuanceColumns } from '@/components/_pages/jdk/issuance/utils/config';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import type { IssuanceItem } from '@/components/_pages/jdk/issuance/utils/type';
import type { ConnectState } from '@/models/connect';
import { useSelector } from 'umi';

const Index = () => {
  const actionRef = useRef<ActionType>();
  const { winSize } = useSelector((state: ConnectState) => state.global);

  const handleSwitch = (record: IssuanceItem, checked: boolean) => {
    console.log(record, checked);
    const { auth_id } = record;
    setSftcCreateShop({ auth_id, is_close: checked ? '1' : '0' }).then((res) => {
      const { code, msg } = res;
      if (code == 0) {
        message.success('操作成功');
        actionRef.current?.reload();
      } else {
        message.error(msg);
      }
    });
  };

  const operateColumns: ProColumns<IssuanceItem>[] = [
    {
      title: '操作',
      valueType: 'option',
      width: winSize === 'xs' ? 60 : 140,
      fixed: 'right',
      align: 'center',
      ellipsis: true,
      render: (_, record) => {
        const { is_close } = record;
        return <Switch checked={is_close == '1'} onChange={handleSwitch.bind(null, record)} />;
      },
    },
  ];

  return (
    <PageContainer>
      <DragTable>
        <ProTable
          cardProps={{
            title:
              '注：开启后该城市的商家发单使用顺丰同城城市账号，未开启则用商户自己账号发单，现有逻辑不变',
            extra: [
              <Button type='primary' onClick={() => actionRef.current?.reload()}>
                刷新
              </Button>,
            ],
            headStyle: {
              padding: '16px 24px',
              flexDirection: winSize === 'xs' ? 'column' : 'unset',
              alignItems: 'end',
            },
          }}
          rowKey='open_id'
          actionRef={actionRef}
          request={accountSftcList}
          columns={[...issuanceColumns, ...operateColumns]}
          toolBarRender={false}
          search={false}
          pagination={false}
          scroll={{
            x: 640,
          }}
        />
      </DragTable>
    </PageContainer>
  );
};

export default Index;
